-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[utils] Add component propType #13816
[utils] Add component propType #13816
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my perspective, we only miss process.env.NODE_ENV
checks to obliviate the bundle size implication. It would also be great to use it everywhere. Related to facebook/prop-types#200.
@@ -149,6 +149,10 @@ function generatePropType(type) { | |||
return generatePropType(chained); | |||
} | |||
|
|||
if (type.raw === 'componentProp') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, nice trick!
Yep need to investigate the bundle size increase. I was hoping it would be fully wrapped due to |
We don't handle it, at least not with the wrap mode. I have been using the following pattern so far: |
The issue is again that we don't transpile to esmodules. Lines like |
What's the link? I agree that we should push the tree shaking story forward!
If it wasn't for |
If you still want to support imports via There is an alternative with |
Why would we want to make nested import tree-shakeable? A top level tree shakeable package sounds good enough to me. |
bf358af
to
d60d0f4
Compare
Regarding the tree shaking. I think that we can try the react-router approach. They use a full path import where the path change between the esmodule and the commonjs version: |
d60d0f4
to
3ab7fd3
Compare
3ab7fd3
to
8da2fdf
Compare
8da2fdf
to
58faaeb
Compare
The goal should be to make everything tree-shakeable. You don't want to bother yourself with package structure everytime you import something. If you make the top level tree-shakeable you should also make everything below tree-shakeable. You want everything that is importing something to be tree-shakeable. It's the reason why we have a bundle increase in That's one of the benfits of tree shaking. Bundle size never increases unless something observable changes. Right now it can increase if any of the packages imported in |
@eps1lon Oh, I haven't realized that tree-shaking isn't transitive. That it will work for |
TL;DR: Given a file In general tree-shaking will only work (in webpack) with esmodules. The key difference is esmodules use static imports via So webpack knows that it can eliminate all imports in var _utils = _interopRequireDefault(require("@material-ui/utils"));
var componentProp = _utils.componentProp; and webpack will bundle the full package. It would be unsafe to shake everything else from var _utils = _interopRequireDefault(require("@material-ui/utils"));
var componentProp = _utils.componentProp;
var myUtilsFunction = _utils[someInputStringFromTheRuntime]; // maybe at runtime this would evaluate to 'exactProp'
// unsafe tree shaking would result in _utils: { componentProps: Function, exactProp: undefined }
myUtilsFunction(); // TypeError: myUtilsFunction is not callable With #13391 however this would be transpiled to (or rather no transpilation at import level) I'll rebase #13391 and update eps1lon/material-ui-treeshaking. If I understood this correctly then I should be able to demonstrate the difference in bundle size on the |
It may be obvious from the prop name what's expected but using
react-is
is more robust against future type changes of accepted values forReact.createElement
. It also provides a more helpful error message in case something different was passed. React already has a pretty helpful message but it will help narrow down the issue in case we apply some conditional logic to thecomponent
prop e.g. inListItem
.Todo: